import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickerWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ImagePickerState();
  }
}

class _ImagePickerState extends State<ImagePickerWidget> {
  var _imgPath;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("相机和相册")),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            _ImageView(_imgPath, context),
            SizedBox.fromSize(size: Size.fromHeight(50)),
            ElevatedButton(onPressed: _takePhoto, child: Text("拍照")),
            SizedBox.fromSize(size: Size.fromHeight(50)),
            ElevatedButton(onPressed: _openGallery, child: Text("选择照片")),
          ],
        ),
      ),
    );
  }

  /*图片控件*/
  Widget _ImageView(var imgPath, BuildContext context) {
    if (imgPath == null) {
      return Center(child: Text("请选择图片或拍照"));
    } else {
      return Center(
        child: Image.file(
          imgPath,
          width: MediaQuery.of(context).size.width,
          //        height: 100,
        ),
      );
    }
  }

  /*拍照*/
  _takePhoto() async {
    // var image = await ImagePicker().getImage(source: ImageSource.camera);

    // setState(() {
    //   _imgPath = image;
    // });

    final ImagePicker picker = ImagePicker();
    final LostDataResponse response = await picker.retrieveLostData();
    if (response.isEmpty) {
      return;
    }
    final List<XFile>? files = response.files;
    if (files != null) {
    } else {}
  }

  /*相册*/
  _openGallery() async {
    // var image = await ImagePicker().getImage(source: ImageSource.gallery);
    // setState(() {
    //   _imgPath = image;
    // });
  }
}
